home *** CD-ROM | disk | FTP | other *** search
- Office Automation Overview
- --------------------------
-
- There are 4 related Office classes built-in to REALbasic.
- Please note that these new classes will conflict with the old Office plugins,
- so make sure to remove the Office plugins from your plugins folder before
- launching REALbasic.
-
- Office class
- - This class contains all the enums you'd need for Office automation
-
- ExcelApplication class
- - This class is used to automate Excel
-
- PowerPointApplication class
- - This class is used to automate PowerPoint
-
- WordApplication class
- - This class is used to automate Word
-
-
- Office Automation Known Issues
- ------------------------------
-
- Mac OS 9:
- Not supported
-
- OS X:
- The OLE libraries are stored privately within Office, so you
- must compile your application, and copy it to the Office folder
- inside the Microsoft Office X parent folder.
- i.e. inside Mac OS X: Applications: Microsoft Office X: Office
- For debugging, you need to copy the REALbasic IDE inside the Office folder.
- We are working on getting apps to run outside the Office folder.
-
-
- Office Automation under REALbasic versus VisualBasic
- ----------------------------------------------------
-
- Working from the Application class:
-
- There is an implied Application instance when you write VBA code from
- within Excel, PowerPoint, or Word. For Example:
-
- Dim pres as Presentation
- Dim slide1 as Slide
-
- Set pres = Presentations.Add
- ' The above is the same as saying:
- ' Set pres = Application.Presentations.Add
-
- Set slide1 = pres.Slides.Add(1, ppLayoutText)
-
- If we're in PowerPoint this code would run just fine since it knows
- what a Presentation object is. Obviously if you typed this code in
- either Word or Excel, it would generate errors. So what does this
- code look like in RB? Here's the RB code:
-
- Dim PowerPoint as new PowerPointApplication
- Dim pres as PowerPointPresentation
- Dim slide1 as PowerPointSlide
-
- pres = PowerPoint.Presentations.Add
- slide1 = pres.Slides.Add(1, Office.ppLayoutText)
-
- There's really only two differences here. First off, all PowerPoint
- objects are prefixed with "PowerPoint", all Word objects are prefixed
- with "Word", and all Excel objects are prefixed with "Excel".
- Another difference is that all constant values are in the Office module,
- this is done so we don't clutter the namespace in RB.
-
-
- Passing parameters by name:
-
- RB doesn't support passing parameters by name, however you can still
- achieve this with a bit of work. First of all, you have to understand
- how to use the OLEObject. You can read up on the docs, but for brevity
- here's an example that you can model after:
-
- Let's record a find and replace macro in Word and translate it to RB
-
- Selection.Find.ClearFormatting
- Selection.Find.Replacement.ClearnFormatting
- With Selection.Find
- .Text = "find this"
- .Replacement.Text = "replace with"
- .Wrap = wdFindContinue
- .Format = false
- .MatchCase = false
- .MatchWholeWord = false
- .MatchWildcards = false
- .MatchSoundsLike = false
- .MatchAllWordForms = false
- End With
- Selection.Find.Execute Replace:=wdReplaceAll
-
- Ok, here's what it would look like in RB
-
-
- Dim word as new WordApplication
- Dim find as WordFind
-
- find = word.Selection.Find
-
- find.ClearFormatting
- find.Replacement.ClearFormatting
- find.text = "find this"
- find.Replacement.Text = "replace with"
- find.Wrap = Office.wdFindContinue
- find.Format = false
- find.MatchCase = false
- find.MatchWholeWord = false
- find.MatchWildcards = false
- find.MatchSoundsLike = false
- find.MatchAllWordForms = false
-
- // Now the fun stuff
-
- Dim replaceParam as new OLEParameter
-
- replaceParam.Value = Office.wdReplaceAll
- // according to the docs on Find.Execute the Replace parameter is the 11th
- replaceParam.Position = 11
-
- find.Execute replaceParam
-
- That's all there is too it. Obviously the most painful bit is finding the
- correct position of that named parameter. That's about the only time when you
- really need to launch VBA and look it up in their Object Browser.
-
-
- Conflicting keywords:
-
- There are certain reserved keywords in RB (these will usually be lighted in
- a different color, such as 'Select' or 'End') that cannot be used as method
- names or property names. Unfortunately, Excel, as an example, utilitizes
- some of these names in their methods/properties. To get around this problem,
- you can suffix the method/property name that you want access to with an
- underscore character. Here's an example:
-
- Excel.Range("A1", "A3").Select_
-
- Since the keyword 'Select' is reserved, you can suffix that with an
- underscore character and RB will hand it off to Excel as "Select".
-
-
- Loading Office docs
- -------------------
-
- If our autocomplete is not sufficient, or you want a higher level view of the
- Office classes, you can load up VBA and follow the Object Browser to see what
- each class supports. If you're using the Windows IDE, you can view the Office
- Type Libraries, which are essentially the docs for Word, Excel, and PowerPoint.
- To load these up, go to File | Add ActiveX Components... and under the
- "References" tab, you can select the appropriate type library to view.
- For example, "Microsoft Word #.# Object Library" contains the docs for Word,
- where #.# is the version number.
-
-
- Trouble Shooting
- ----------------
-
- Q: How do I know if I have the OLE libraries installed?
- A: One easy way is to load up Visual Basic Editor (under the Tools
- and Macros menu), and do some automation with VBA.
- Here are the steps you can use to automate PowerPoint from Word:
-
- 1. Load up Word
- 2. Load up Visual Basic Editor in Word
- 3. Insert a UserForm
- 4. Add a CommandButton to the form
- 5. In the click event of the button, put in this code:
- Dim obj as Object
- Set obj = CreateObject("PowerPoint.Application")
- obj.Activate
- 6. Run the program and click on the button
- 7. If PowerPoint loads up and you don't get any errors, then the OLE
- libraries are installed.
-
- Q: What are the possible errors I can catch?
- A: Errors come through OLE, so you need to catch OLEExceptions.
- This will report the last command that failed along with any
- additional information about the exception.
-
- Dim word as new WordApplication
-
- word.ShowClipboard
-
- exception err as OLEException
- msgbox err.message
-
-
- Last Updated: February 19, 2004
-